package org.gary.corpration.action;

import static lengreen.util.constant.Constant.DateFomaterString.YYYY_MM_DD_HH_MM_SS;
import static org.gary.core.util.Utils.makeDateFormateJsonConfig;
import static org.gary.core.util.Utils.parseIndex;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.gary.core.service.InitService;
import org.gary.corpration.model.Corpration;
import org.gary.corpration.service.CorprationService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 企业信息 Action
 * @author Gary,lengreen
 * 
 */
@Component
@Scope("prototype")
public class CorprationAction extends ActionSupport implements
		ServletRequestAware, SessionAware,
		ServletResponseAware {

	private static final long serialVersionUID = -9186155854636394488L;
	private HttpServletRequest request;
	private HttpServletResponse response;
	private Map<String, Object> session;
	
	private CorprationService corprationService;
	private InitService initService;
	private Corpration corpration;
	private List<Corpration> corprationList;
	/**分页开始*/
	private int start;
	/**分页最大记录数*/
	private int limit;
	/**是否可见*/
	private String visible;
	private String result;

	/**
	 * 列出所有administrator	
	 * @throws IOException
	 */
	public void list() throws IOException {
		start = parseIndex(start, limit);
		Map<String, Object> where = new HashMap<String, Object>();
		where.put(" entity.visible = ", new Boolean(getVisible()));
		setCorprationList(corprationService.list(Corpration.class, start,
				limit, where, new Object[] { " entity.id desc " }));

		for (Corpration c : corprationList) {
			System.out.println("cccc-->" + c);
		}

		JsonConfig config = makeDateFormateJsonConfig(YYYY_MM_DD_HH_MM_SS);
		config.setExcludes(new String[] { "culture", "aboutUs", "prospects" });
		JSONArray corprationListJsonArray = JSONArray.fromObject(corprationList, config);
		JSONObject corprationJsonObject = new JSONObject();
		corprationJsonObject.put("corprationTotal", corprationService
				.selectTotalCount(null));
		corprationJsonObject.put("corprationList", corprationListJsonArray);
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(corprationJsonObject.toString());
	}

	/**
	 * 加载一个administrator	
	 * @throws IOException
	 */
	public void load() throws IOException {
		Corpration p = corprationService.load(Corpration.class, corpration
				.getId());
		JSONObject corprationJsonObject = JSONObject.fromObject(p,
				makeDateFormateJsonConfig(YYYY_MM_DD_HH_MM_SS));
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(corprationJsonObject.toString());
	}

	/**
	 * 更新一个administrator	
	 * @throws IOException
	 */
	public void update() throws IOException {
		if (corpration != null) {
			corprationService.update(corpration);
			result = "{'success':'true'}";
		} else {
			result = "{'success':'false'}";
		}
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(result);
	}

	@Resource
	public void setInitService(InitService initService) {
		this.initService = initService;
	}

	@Resource
	public void setCorprationService(CorprationService corprationService) {
		this.corprationService = corprationService;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		this.request = arg0;
	}

	public String about() {
		initService.initAll(session);
		Corpration about = corprationService.getAboutus(session
				.get("WW_TRANS_I18N_LOCALE"));
		request.setAttribute("about", about);
		return SUCCESS;
	}
	
	public String connectUs(){
		initService.initAll(session);
		Corpration connectUs = corprationService.getAboutus(session
				.get("WW_TRANS_I18N_LOCALE"));
		request.setAttribute("connectUs", connectUs);
		return SUCCESS;
	}

	public void recover() throws IOException {
		try {
			if (corpration != null) {
				corprationService.update(Corpration.class, new Object[] { corpration
						.getId() }, true);
				result = "{'success':'true'}";
			} else {
				result = "{'success':'false'}";
			}
			response.setCharacterEncoding("utf-8");
			response.getWriter().write(result);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public void setCorpration(Corpration corpration) {
		this.corpration = corpration;
	}

	public Corpration getCorpration() {
		return corpration;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getStart() {
		return start;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getLimit() {
		return limit;
	}

	public void setCorprationList(List<Corpration> corprationList) {
		this.corprationList = corprationList;
	}

	public List<Corpration> getCorprationList() {
		return corprationList;
	}

	public void setVisible(String visible) {
		this.visible = visible;
	}

	public String getVisible() {
		return visible;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
}
